package com.hengpeng.itfints.service.payment.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.common.util.DateTimeUtil;
import com.hengpeng.itfinbase.persist.Payment;
import com.hengpeng.itfinbase.persist.Payment.PayStatus;
import com.hengpeng.itfinbase.persist.Payment.PaymentType;
import com.hengpeng.itfints.service.ServiceFactory;
import com.hengpeng.itfints.service.payment.PaymentService;

@Lazy
@Service
public class PaymentServiceImpl implements PaymentService
{
	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public List<Payment> queryMyList(Long userId, PaymentType type, String status,String queryTime)
	{
		DynamicQuery query = new DynamicQuery(Payment.class);
		query.eq("userId", userId);
		if (type != null)
		{
			query.eq("type", type);
		}
		else
		{// 默认显示充值记录
			query.eq("type", PaymentType.IMPREST);
		}
		if ("SUCCESS".equals(status)) {
			query.inArray("status", new PayStatus[]{PayStatus.SUCCESS});
		} else if ("DEALING".equals(status)) {
			query.inArray("status", new PayStatus[]{PayStatus.PAYING});
		} else if ("FAIL".equals(status)) {
			query.inArray("status", new PayStatus[]{PayStatus.FAILED});
		}
		if ("three_day".equals(queryTime)) {//查询三天以内的记录
			query.ge("requestTime", DateTimeUtil.addDate(new Date(), -3));
		} else if ("one_week".equals(queryTime)) {//查询一周以内的记录
			query.ge("requestTime", DateTimeUtil.addDate(new Date(), -7));
		} else if ("one_month".equals(queryTime)) {//查询一个月之内的记录
			query.ge("requestTime", DateTimeUtil.addDate(new Date(), -30));
		} else if ("three_month".equals(queryTime)) {//查询三个月之内的记录
			query.ge("requestTime", DateTimeUtil.addDate(new Date(), -90));
		} else {//默认显示一周的记录
			query.ge("requestTime", DateTimeUtil.addDate(new Date(), -7));
		}
		query.desc("requestTime");
		query.setQueryType(QueryType.JDBC);
//		System.out.println(query.getSqlResult().getSql());
		return serviceFactory.getPersistService().find(Payment.class, query);
	}
}
